//
//  WorkDetailViewController.m
//  LubanWork
//
//  Created by xiaotong on 2020/1/2.
//  Copyright © 2020 xiaohao. All rights reserved.
//

#import "WorkDetailViewController.h"
#import "WorkInfoTableViewCell.h"
#import "HotLineTableViewCell.h"
#import "ColletionSelectModel.h"
#import "WorkContentTableViewCell.h"
#import "CompanyInfoTableViewCell.h"
#import "FooterView.h"
#import "sureView.h"
#import "GotoLoginView.h"
#import "WorkModel.h"
@interface WorkDetailViewController ()<UITableViewDataSource,UITableViewDelegate>{
    CGFloat hes;
    CGSize hes1;
}
@property (nonatomic,strong)UITableView *tableview;
@property (nonatomic,strong)NSArray *sectionName;

@property (nonatomic,strong)FooterView *bottomView;

@property(nonatomic,strong) WorkModel * workModel;

@property(nonatomic,strong) ColletionSelectModel * selectModel;
@end

@implementation WorkDetailViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self loadData];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"职位详情";
    self.sectionName = @[@"",@"",@"",@"",@""];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
        make.top.left.right.mas_offset(0);
//        make.height.mas_offset(HEIGHT-kTabHeight -50);
        make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-50-BOTTOM_HEIGHT);
    }];
    FooterView *bottom = [[FooterView alloc] init];
    [bottom.FavoriteBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottom.SignBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    _bottomView = bottom;
//    _bottomView.SignBtn.selected = YES;
    [self.view addSubview:bottom];
    [bottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tableview.mas_bottom).mas_offset(0);
        make.height.mas_offset(50);
        make.width.mas_offset(WIDTH);
        
    }];
    self.view.backgroundColor = kViewBgColor;
}

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-kTabHeight-50) style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundColor = kColorFromRGB(250, 250, 255);
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.estimatedRowHeight = 213;
        _tableview.rowHeight = UITableViewAutomaticDimension;
    }
    return _tableview;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(FooterView *)bottomView{
    if (!_bottomView) {
        
        _bottomView = [[FooterView alloc] init];
    }
    return _bottomView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return kSCRATIO(79);
    }else if (indexPath.section == 1){
        return kSCRATIO(45);
    }else if (indexPath.section == 2){
//        return kSCRATIO(320+self.sizeWorkHeight.height);
        return UITableViewAutomaticDimension;
        
    }else if (indexPath.section == 3){
        return kSCRATIO(115);
    }
    return kSCRATIO(79);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.rowHeight = UITableViewAutomaticDimension;
    if (indexPath.section == 0) {
        WorkInfoTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[WorkInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        NSLog(@"%@",self.workModel.result.name);
        
        cell.workNameLab.text = self.workModel.result.name;
        cell.moneyOfDayLab.text = [self.workModel.result.wagemax stringValue];
        NSString *str1 = [self.workModel.result.wagetype isEqualToNumber:@1] == YES ? @"日结":@"月结";
        
        NSString *str2 = [self.workModel.result.jobtime isEqualToNumber:@1] == YES ? @"长期":@"短期";
        NSString *str3;
        if ([self.workModel.result.sex isEqualToNumber:@1]) {
            str3 = @"男";
        }else if ([self.workModel.result.sex isEqualToNumber:@2]){
            str3 = @"女";
        }else{
            str3 = @"男女不限";
        }
        
        cell.workTypeLab.text = [NSString stringWithFormat:@"%@ | %@ | %@",str1,str2,str3];
        [cell setMoneyWithString:[self.workModel.result.wagemax stringValue] WithType:WorkStyleTypeDays];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if(indexPath.section == 1){
        HotLineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hotcell"];
        if (!cell) {
            cell = [[HotLineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hotCell"];
        }
        if ([UtilTools isBlankString:self.workModel.result.companyWx]) {
            cell.numberLab.text=self.workModel.result.companyQq;
            cell.TypeImge.image=[UIImage imageNamed:@"qq"];
        }else{
            cell.numberLab.text = self.workModel.result.companyWx;
            cell.TypeImge.image=[UIImage imageNamed:@"WeChat"];
        }
        [cell.copyBtn addTarget:self action:@selector(copyNumber) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 2){
        WorkContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"workcell"];
        if (!cell) {
            cell = [[WorkContentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"workcell"];
        }
        //        WorkContentLabWorkContentLabWorkContentLab
        NSString *str1 = [self.workModel.result.wagetype isEqualToNumber:@1] == YES ? @"日结":@"月结";
        
        NSString *str2 = [self.workModel.result.jobtime isEqualToNumber:@1] == YES ? @"长期":@"短期";
        NSString *str3;
        if ([self.workModel.result.sex isEqualToNumber:@1]) {
            str3 = @"男";
        }else if ([self.workModel.result.sex isEqualToNumber:@2]){
            str3 = @"女";
        }else{
            str3 = @"男女不限";
        }
        
//        cell.WorkContentLab.text = [NSString stringWithFormat:@"%@ | %@ | %@",str1,str2,str3];
        cell.WorkContentLab.text = self.workModel.result.jobdescribe;
        cell.WorkJoinTypeLab.text = [NSString stringWithFormat:@"%@ | %@ | %@",str1,str2,str3];
        
        cell.TeamNumberLab.text = [self.workModel.result.signup stringValue];
        
        cell.Timelab.text =  self.workModel.result.worktime;
        cell.mapLab.text = self.workModel.result.jobsite;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        
        return cell;
    }else if (indexPath.section == 3){
        CompanyInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
        if (!cell) {
            cell = [[CompanyInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"infoCell"];
        }
        [cell.compnyImage sd_setImageWithURL:[NSURL URLWithString:self.workModel.result.companyImage] placeholderImage:[UIImage imageNamed:@""]];
        cell.companyName.text = self.workModel.result.companyName;
        [cell setScoreWithNumber:[self.workModel.result.score integerValue]];
        
        //        cell.b
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    return [UITableViewCell new];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    if ( section == 3 ){
//        return kSCRATIO(50);
//    }
    return 0.01;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kSCRATIO(5);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    if (section == 3) {
//
//    }
//    return [UIView new];
//}

-(void)clickAction:(UIButton *)sender{
    
    UserModel *model = [UserModel sharedInstance:UserKey0];
    __weak typeof(self) weakSelf = self;
    if (_bottomView.FavoriteBtn == sender) {
        DLog(@"关注");
        
        
        if ([UtilTools isBlankString:model.token]) {
            [GotoLoginView gotoLoginViewController];
            return;
        }
        _bottomView.FavoriteBtn.selected = !sender.isSelected;
        NSString *url = [NSString stringWithFormat:@"%@",_bottomView.FavoriteBtn.selected == NO?@"api/favorite/remove":@"api/favorite/add"];
        
        [[NetWorkManager sharedManager] getData:url parameters:@{
            @"token":model.token,
            @"jz_id":self.detailID
        } success:^(NSURLSessionDataTask *task, id responseObject) {
            if ([responseObject[@"code"] isEqualToNumber:@200]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                });
            }
            
            
            
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
            
        }];
    }else if (_bottomView.SignBtn == sender){
        DLog(@"立即报名");
        
        if ([UtilTools isBlankString:model.token]) {
            [GotoLoginView gotoLoginViewController];
            return;
        }
        _bottomView.SignBtn.selected = !sender.isSelected;
        if (_bottomView.SignBtn.selected) {
            
            sureView *view = [[sureView alloc] init];
            if ([UtilTools isBlankString:self.workModel.result.worktime]) {
                self.workModel.result.worktime = @"";
            }
            if ([UtilTools isBlankString:self.workModel.result.jobsite]) {
                self.workModel.result.jobsite = @"";
            }
            view.arr = @[weakSelf.workModel.result.worktime,weakSelf.workModel.result.jobsite];
            [view show:^(BOOL isOrNo) {
                if (isOrNo) {
                    GotoLoginView *gologin = [[GotoLoginView alloc] init];
                    gologin.weChatLab.text = weakSelf.workModel.result.companyWx;
                    DLog(@"确定立即报名");
                    [[NetWorkManager sharedManager]getData:@"api/jianzhi/addSignup" parameters:@{
                        @"jzId":self.detailID
                    }
                                                    success:^(NSURLSessionDataTask *task, id responseObject) {
                        if ([responseObject[@"code"] isEqualToNumber:@200]) {
                            dispatch_async(dispatch_get_main_queue(), ^{
                                if (![UtilTools isBlankString:weakSelf.workModel.result.companyWx] || ![weakSelf.workModel.result.companyQq isEqualToString:@"请联系下方电话"]) {
                                    [gologin show];
                                }else{
                                    [HWProgressHUD showMessage:@"请查看电话"];
                                }
                                _bottomView.SignBtn.selected = YES;
                                
                                
                            });
                        }else{
                            dispatch_async(dispatch_get_main_queue(), ^{
                                _bottomView.SignBtn.selected = NO;
                                [gologin dismiss];
                            });
                        }
                    } failure:^(NSURLSessionDataTask *task, NSError *error) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            _bottomView.SignBtn.selected = NO;
                            [gologin dismiss];
                        });
                    }];
                }else{
                    _bottomView.SignBtn.selected = NO;
                }
            }];
        }else{
            UserModel *user = [UserModel sharedInstance:UserKey0];
            if (!user.isLogin) {
                [GotoLoginView gotoLoginViewController];
                return;
            }
            DLog(@"取消报名");
            _bottomView.SignBtn.selected = YES;
            _bottomView.SignBtn.enabled = YES;
            return;
            //            postJsonWithURLString
            //            [[NetWorkManager sharedManager] postJsonWithURLString:@"api/jianzhi/updateSignup" parameters:@{
            //                @"jzId":self.detailID,
            //                @"signupStatus":@"2"
            //            } success:^(id responseObject) {
            //
            //            } failure:^(NSError *error) {
            //
            //            }];
//            [[NetWorkManager sharedManager] postData:@"api/jianzhi/updateSignup" parameters:@{
//                @"jzId":self.detailID,
//                @"signupStatus":@"2"
//            } success:^(NSURLSessionDataTask *task, id responseObject) {
//                if ([responseObject[@"code"] isEqualToNumber:@200]) {
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        _bottomView.SignBtn.selected = NO;
//                    });
//                }else{
//                    dispatch_async(dispatch_get_main_queue(), ^{
//
//                        _bottomView.SignBtn.selected = YES;
//                    });
//                }
//            } failure:^(NSURLSessionDataTask *task, NSError *error) {
//
//            }];
            
        }
        
    }
}

-(void)loadData{
    
    __weak typeof(self) weakSelf = self;
    [[NetWorkManager sharedManager]getData:@"api/jianzhi/getJianzhiBykey" parameters:@{
        @"jzId":self.detailID
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            self.workModel = [WorkModel yy_modelWithJSON:responseObject];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableview reloadData];
            
            });
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    UserModel *user = [UserModel sharedInstance:UserKey0];
    NSLog(@"%@",user.token);
    NSLog(@"%@",self.detailID);
    
    [[NetWorkManager sharedManager]getData:@"api/jianzhi/getSignupFavoriteByUser" parameters:@{
        @"jzId":self.detailID
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            self.selectModel = [ColletionSelectModel yy_modelWithJSON:responseObject];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableview reloadData];
                if ([self.selectModel.result.favorite isEqualToNumber:@1]) {
                    weakSelf.bottomView.FavoriteBtn.selected = YES;
                }else{
                    weakSelf.bottomView.FavoriteBtn.selected = NO;
                }
                
                if ([self.selectModel.result.signupStatus isEqualToNumber:@1]) {
                    weakSelf.bottomView.SignBtn.selected = YES;
                }else{
                    weakSelf.bottomView.SignBtn.selected = NO;
                }
            });
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
-(void)copyNumber{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    if ([UtilTools isBlankString:self.workModel.result.companyWx]) {
        if ([self.workModel.result.companyQq isEqualToString:@"请联系下方电话"]) {
            [HWProgressHUD showMessage:@"请联系下方电话"];
            return;
        }
        pasteboard.string = [NSString stringWithFormat:@"%@",self.workModel.result.companyQq];
    }else{
        pasteboard.string = [NSString stringWithFormat:@"%@",self.workModel.result.companyWx];
    }
}


 
@end
